package com.kjsdfgk.hjmdsf.favorite.mvp.ui.activity;

import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kjsdfgk.hjmdsf.app.entity.FavoriteItem;
import com.kjsdfgk.hjmdsf.app.entity.HistoryItem;
import com.lksdkjs.live.R;

import org.litepal.LitePal;

import java.util.List;

public class FavoriteAdapter extends BaseQuickAdapter<FavoriteItem, BaseViewHolder> {
    public FavoriteAdapter(int layoutResId) {
        super(layoutResId);
    }
    @Override
    protected void convert(BaseViewHolder helper, FavoriteItem item) {

        TextView title = helper.getView(R.id.title);
        title.setText(item.title);
        CheckBox checkBox = helper.getView(R.id.favorite);
        checkBox.setChecked(true);
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){
                FavoriteItem favoriteItem =  JSON.parseObject(JSON.toJSONString(item),FavoriteItem.class);
                favoriteItem.time = TimeUtils.getNowString();
                favoriteItem.save();

            }else {
                List<FavoriteItem> itemList = LitePal.where("url=? and userName=?",item.url,item.userName).find(FavoriteItem.class);
                for (FavoriteItem favoriteItem : itemList){
                    favoriteItem.delete();
                }

            }
        });
        ImageView simpleDraweeView = helper.getView(R.id.web_img);

        Glide.with(helper.itemView).load(item.img).placeholder(R.mipmap.img_loading).transition(new DrawableTransitionOptions().crossFade(300)).into(simpleDraweeView);


    }
}
